//logs.js
const util = require('../../utils/util.js')
const { url } = require('../../config/index.js') // 接口请求地址

Page({
  // 页面的初始数据
  data: {
    words: '', // 意见
    defaultSize: 'default',
    primarySize: 'default',
    warnSize: 'default',
    disabled: false,
    plain: false,
    loading: false,
    tags: [
      "摆明吐槽",
      "体验差",
      "金额出错"
    ],
    tagSlect: [], // 标签选择索引
    tagselect: {} // 标签激活样式
  },
  // 文本输入
  textInput(e) {
    let word = e.detail.value
    // console.log(word)
    this.setData({
      words: word
    })
    // console.warn(this.data.words)
  },
  tagClick (e) {
    let index = e.currentTarget.dataset.index
    if(this.data.tagSlect.indexOf(index) > -1){
      let tag = this.data.tagSlect
      tag.splice(this.data.tagSlect.indexOf(index), 1)
      let tagObj = this.data.tagselect
      tagObj[index] = false
      this.setData({ tagSlect: tag, tagselect: tagObj })
      console.log(tagObj)
    } else {
      let tag = this.data.tagSlect
      tag.push(index)
      let tagObj = this.data.tagselect
      tagObj[index] = true
      this.setData({ tagSlect: tag, tagselect: tagObj })
      console.log(tagObj)
      
    }
  },
  // 提交意见
  submit() {
    if (this.data.loading) return
    this.setData({
      loading: true
    })
    let mosaic = ''
    for (let x in this.data.tagselect ) {
      if (this.data.tagselect[x]) {
        mosaic += this.data.tags[x]
      }
    }
    wx.request({
      url: url + 'SystemSet/addFeedback.action',
      method: 'POST',
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      data: {
        USER_ID: wx.getStorageSync('userInfo').USER_ID,
        TYPE: 0,
        CONTENT: mosaic + this.data.words
      },
      success: res => {
        this.setData({
          loading: false
        })
        if (res.data.STATUS_CODE == 1) {
          wx.showToast({
            title: '提交成功',
            icon: 'success',
            duration: 2000
          })
          setTimeout( () => {
            wx.navigateBack({
              delta: 1
            })
          }, 2000)
        } else {
          wx.showToast({
            title: '提交失败',
            icon: 'success',
            duration: 2000
          })
        }

      }
    })  
  },
  // 生命周期函数--监听页面加载
  onLoad: function (options) {
    // Do some initialize when page load.
  },
  // 生命周期函数--监听页面初次渲染完成
  onReady: function () {
    // Do something when page ready.
  },
  // 生命周期函数--监听页面显示
  onShow: function () {
    // Do something when page show.
  },
  // 生命周期函数--监听页面隐藏
  onHide: function () {
    // Do something when page hide.
  },
  // 生命周期函数--监听页面卸载
  onUnload: function () {
    // Do something when page close.
  },
  // 页面相关事件处理函数--监听用户下拉动作
  onPullDownRefresh: function () {
    // Do something when pull down.
  },
  // 页面上拉触底事件的处理函数
  onReachBottom: function () {
    // Do something when page reach bottom.
  },
  // 用户点击右上角转发
  onShareAppMessage: function () {
    // return custom share data when user share.
  },
  // 页面滚动触发事件的处理函数
  onPageScroll: function () {
    // Do something when page scroll
  },
  // 当前是 tab 页时，点击 tab 时触发
  onTabItemTap () {
    // code...
  }
})
